package com.quanyan.place.biz.impl;

import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.alibaba.fastjson.JSON;
import com.quanyan.api.APIResponse;
import com.quanyan.common.Plugin.Page;
import com.quanyan.common.constants.Constants;
import com.quanyan.common.utils.DateUtils;
import com.quanyan.place.biz.IBaseService;
import com.quanyan.place.biz.ISearchService;
import com.quanyan.place.cache.PlaceRedisTemplate;
import com.quanyan.place.constants.PlaceConstants;
import com.quanyan.place.entity.*;
import com.quanyan.place.entity.apireq.ReqSearchVo;
import com.quanyan.place.entity.apireq.ReqTime;
import com.quanyan.place.entity.apiresp.RespBizPlaceBaseInfo;
import com.quanyan.place.entity.apiresp.RespCategory;
import com.quanyan.place.entity.apiresp.RespCity;
import com.quanyan.place.entity.apiresp.RespDict;
import com.quanyan.place.entity.base.apiresp.RespDistrict;
import com.quanyan.place.mapper.DictMapper;
import com.quanyan.place.mapper.TbBizPlaceMapper;
import com.quanyan.place.mapper.TbBizPlaceUnitSalePlanMapper;
import com.quanyan.search.entity.request.ReqPlaceSearchInfo;
import com.quanyan.search.entity.request.ReqPlaceSearchInfoNew;
import com.quanyan.search.entity.response.PlaceSearchResponse;
import com.quanyan.search.entity.response.SearchResponses;
import com.quanyan.search.entity.vo.PlaceIndexInfo;
import com.quanyan.search.service.PlaceSearchService;
import com.quanyan.search.service.impl.PlaceSearchServiceImpl;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.collections.map.HashedMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.validation.constraints.NotNull;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by jingliu on 16/6/1.
 */
@Service
public class SearchServiceImpl implements ISearchService {
    private final static Logger logger = LoggerFactory.getLogger(SearchServiceImpl.class);
    @Autowired
    private DictMapper dictMapper;

    @Autowired
    private IBaseService baseService;

    @Autowired
    private TbBizPlaceUnitSalePlanMapper tbBizPlaceUnitSalePlanMapper;

    @Autowired
    private TbBizPlaceMapper tbBizPlaceMapper;

    @Value("${SOLRCLOUD.SERVER.URL}")
    private String solrUrl;

    @Autowired
    PlaceRedisTemplate placeRedisTemplate;

    @Override
    public List<RespDict> getSearchSort() {
        DictExample dictExample = new DictExample();
        dictExample.createCriteria().andTagGroupEqualTo(Constants.COMMENT_PLACE_SEARCH_ORDER_ELEMENTS);
        dictExample.setOrderByClause(" priority asc");
        List<Dict> list = dictMapper.selectByExample(dictExample);

        if (CollectionUtils.isNotEmpty(list)) {
            List<RespDict> respDicts = new ArrayList<RespDict>();
            for (Dict dict : list) {
                RespDict respDict = new RespDict();
                BeanUtils.copyProperties(dict, respDict);
                respDict.setId(Integer.parseInt(dict.getDataKey()));
                respDict.setName(dict.getDataValue());
                respDicts.add(respDict);
            }
            return respDicts;
        } else {
            logger.error("字典表中没有可用的排序条件");
            return null;
        }
    }

    private Map<Integer, TbBizPlace> getTbBizPlaceMap(List<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)){
            return null;
        }
        TbBizPlaceExample example = new TbBizPlaceExample();
        example.createCriteria().andIdIn(ids);
        List<TbBizPlace> tbBizPlaces = tbBizPlaceMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(tbBizPlaces)){
            return null;
        }
        Map<Integer, TbBizPlace> map = new HashedMap();
        for (TbBizPlace tbBizPlace : tbBizPlaces){
            map.put(tbBizPlace.getId(), tbBizPlace);
        }
        return map;
    }

    @Override
    public List<RespBizPlaceBaseInfo> getSearchBizPlaceBaseInfos(List<PlaceSearchResponse> placeSearchResponses,
                                                                 ReqSearchVo reqSearchVo, Integer startPosition) {
        if (CollectionUtils.isEmpty(placeSearchResponses)) {
            return null;
        }
        List<RespBizPlaceBaseInfo> respBizPlaceBaseInfos = new ArrayList<>();
//        List<Integer> ids = new ArrayList<>();
//        for (PlaceSearchResponse placeSearchResponse : placeSearchResponses){
//            ids.add(placeSearchResponse.getId());
//        }
//        Map<Integer, TbBizPlace> map = getTbBizPlaceMap(ids);
        for (PlaceSearchResponse placeSearchResponse : placeSearchResponses) {
            RespBizPlaceBaseInfo respBizPlaceBaseInfo = new RespBizPlaceBaseInfo();
            BeanUtils.copyProperties(placeSearchResponse, respBizPlaceBaseInfo);
            //添加签约字段
            respBizPlaceBaseInfo.setIsSign((byte)(placeSearchResponse.getIsSigned().intValue()));
            Float distince = (null == placeSearchResponse.getDistance())? Float.valueOf(0) : placeSearchResponse.getDistance();
            respBizPlaceBaseInfo.setDistance(new Double(distince * 1000));//改成以m为单位
//            respBizPlaceBaseInfo.setDistance(Double.valueOf(distince));//改成以m为单位
            respBizPlaceBaseInfo.setBizPlaceUrl(placeSearchResponse.getBizPlaceUrl());
            respBizPlaceBaseInfo.setGroupId(placeSearchResponse.getGroupId());
            respBizPlaceBaseInfo.setLng(placeSearchResponse.getLng());
            respBizPlaceBaseInfo.setLat(placeSearchResponse.getLat());
            respBizPlaceBaseInfo.setAddress(placeSearchResponse.getAddress());
            respBizPlaceBaseInfo.setPhone(placeSearchResponse.getPhone());
            respBizPlaceBaseInfo.setIsVip(placeSearchResponse.getIsVip());
            if (null != startPosition){
                respBizPlaceBaseInfo.setStartPosition(startPosition);
            }
            //填充场馆基本信息
//            RespBizPlaceBaseInfo respBizPlaceBaseInfo1 = baseService.placeBaseInfoUtils(placeSearchResponse.getId());
//            TbBizPlace tbBizPlace = tbBizPlaceMapper.selectByPrimaryKey(placeSearchResponse.getId());
//            if (!StringUtils.isEmpty(tbBizPlace)){
//                respBizPlaceBaseInfo.setPhone(tbBizPlace.getPhone());
//                respBizPlaceBaseInfo.setAddress(tbBizPlace.getAddress());
//                respBizPlaceBaseInfo.setLat(tbBizPlace.getLat());
//                respBizPlaceBaseInfo.setLng(tbBizPlace.getLng());
//            }
//            if(!StringUtils.isEmpty(reqSearchVo.getMapMark()) && reqSearchVo.getMapMark().intValue()==1){
//                TbBizPlace tbBizPlace = map.get(placeSearchResponse.getId());
//                if (!StringUtils.isEmpty(tbBizPlace)){
//                    respBizPlaceBaseInfo.setPhone(tbBizPlace.getPhone());
//                    respBizPlaceBaseInfo.setAddress(tbBizPlace.getAddress());
////                respBizPlaceBaseInfo.setLat(tbBizPlace.getLat());
////                respBizPlaceBaseInfo.setLng(tbBizPlace.getLng());
//                }
//            }
            //获取场馆经营类型列表
            List<String> categoryNames = placeSearchResponse.getCategoryNames();
            List<RespCategory> respCategories = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(categoryNames)) {
                for(String name : categoryNames){
                    RespCategory respCategory = this.baseService.getRespCategoryByName(name);
                    if (!StringUtils.isEmpty(respCategory)){
                        if (!StringUtils.isEmpty(reqSearchVo.getCategoryId())){
                            if(respCategory.getId().intValue()==reqSearchVo.getCategoryId().intValue()){
                                respCategories.add(0,respCategory);
                            }else{
                                respCategories.add(respCategory);
                            }
                        }else {
                            respCategories.add(respCategory);
                        }
                    }
                }
                respBizPlaceBaseInfo.setCategorys(respCategories);
            }
            //获取城市
            RespCity cityObj = new RespCity();
            cityObj.setId(placeSearchResponse.getCityId());
            cityObj.setName(placeSearchResponse.getCityName());
            respBizPlaceBaseInfo.setCityObj(cityObj);
            //获取地区信息
            RespDistrict distictObj = new RespDistrict();
            distictObj.setCityId(cityObj.getId());
            distictObj.setId(placeSearchResponse.getDistrictId());
            distictObj.setName(placeSearchResponse.getDistrictName());
            respBizPlaceBaseInfo.setDistrictObj(distictObj);

            respBizPlaceBaseInfos.add(respBizPlaceBaseInfo);
        }
        return respBizPlaceBaseInfos;
    }

//    @Override
//    public PlaceSalePlanInfo getPlaceSalePlanInfo(TbBizPlaceUnitSalePlan tbBizPlaceUnitSalePlan) {
//        PlaceSalePlanInfo reqPlaceSalePlan = new PlaceSalePlanInfo();
//        BeanUtils.copyProperties(tbBizPlaceUnitSalePlan, reqPlaceSalePlan);
//        if (tbBizPlaceUnitSalePlan.getStatus().intValue()== PlaceConstants.PLACE_PREORDER_STATUS.intValue()){
//            reqPlaceSalePlan.setSale_flag(PlaceConstants.IS_SALE);
//        }else{
//            reqPlaceSalePlan.setSale_flag(PlaceConstants.IS_NOT_SALE);
//        }
//        reqPlaceSalePlan.setSale_place_id(tbBizPlaceUnitSalePlan.getPlaceId());
//        SimpleDateFormat sf = new SimpleDateFormat(DateUtils.YYYY_MM_DD);
//        reqPlaceSalePlan.setDate(sf.format(tbBizPlaceUnitSalePlan.getDate()));
//        reqPlaceSalePlan.setSalePlanId(tbBizPlaceUnitSalePlan.getId());
//        reqPlaceSalePlan.setStartTime(new SimpleDateFormat(DateUtils.DATE_HH_MM_SS)
//                .format(tbBizPlaceUnitSalePlan.getStartTime()));
//        reqPlaceSalePlan.setEndTime(new SimpleDateFormat(DateUtils.DATE_HH_MM_SS)
//                .format(tbBizPlaceUnitSalePlan.getEndTime()));
//        return reqPlaceSalePlan;
//    }

    @Override
    public ReqPlaceSearchInfo getReqPlaceSearchInfo(ReqSearchVo reqSearchVo) {
        ReqPlaceSearchInfo reqPlaceSearchInfo = new ReqPlaceSearchInfo();
        SimpleDateFormat sf = new SimpleDateFormat(DateUtils.YYYY_MM_DD);
        if (!StringUtils.isEmpty(reqSearchVo.getQuerydate())){
            if (reqSearchVo.getQuerydate().intValue() != 0){
                reqPlaceSearchInfo.setDate(sf.format(new Date(reqSearchVo.getQuerydate())));
            }
        }
        reqPlaceSearchInfo.setDistance(reqSearchVo.getArea());
        reqPlaceSearchInfo.setCategoryId(reqSearchVo.getCategoryId());
        reqPlaceSearchInfo.setCityId(reqSearchVo.getCityId());
        reqPlaceSearchInfo.setDistrictId(reqSearchVo.getDistrictId());
        reqPlaceSearchInfo.setLat(reqSearchVo.getLat());
        reqPlaceSearchInfo.setLng(reqSearchVo.getLng());
        reqPlaceSearchInfo.setCurrentLat(reqSearchVo.getCurrentLat());
        reqPlaceSearchInfo.setCurrentLng(reqSearchVo.getCurrentLng());
        reqPlaceSearchInfo.setPlaceKeyName(reqSearchVo.getKeywords());
        reqPlaceSearchInfo.setSortId(reqSearchVo.getSortType());
        reqPlaceSearchInfo.setIsSigned(reqSearchVo.getIsSign());//扩展签约字段
        reqPlaceSearchInfo.setSearchType(reqSearchVo.getSearchType());
        reqPlaceSearchInfo.setIsVip(reqSearchVo.getIsVip());//是否有会员
        reqPlaceSearchInfo.setIsSaled(reqSearchVo.getIsSaled());
        reqPlaceSearchInfo.setIsShelves(reqSearchVo.getIsShelves());

        List<ReqTime> reqTimes = reqSearchVo.getTimeList();
        if (CollectionUtils.isNotEmpty(reqTimes)){
            List<String> startTimes = new LinkedList<>();
            List<String> endTimes = new LinkedList<>();
            for (ReqTime reqTime : reqTimes){
                String startTime = reqTime.getStartTime();
                String endTime = reqTime.getEndTime();
                if (!StringUtils.isEmpty(startTime) && !StringUtils.isEmpty(endTime)){
                    startTime = startTime.length() == 5 ? startTime + ":00" : startTime;
                    endTime = endTime.length() == 5 ? endTime + ":00" : endTime;
                    startTimes.add(startTime);
                    endTimes.add(endTime);
                }
            }
            reqPlaceSearchInfo.setStartTimeList(startTimes);
            reqPlaceSearchInfo.setEndTimeList(endTimes);
        }
        reqPlaceSearchInfo.setPageNo(reqSearchVo.getPageNum());
        reqPlaceSearchInfo.setPageSize(reqSearchVo.getPageSize());

        return reqPlaceSearchInfo;
    }

    @Override
    public APIResponse<SearchResponses<List<PlaceSearchResponse>>> listSearch(ReqPlaceSearchInfo reqPlaceSearchInfo) {
        PlaceSearchService placeSearchService = new PlaceSearchServiceImpl(solrUrl);
        return placeSearchService.searchPlaceInfoList(reqPlaceSearchInfo);
    }

    public APIResponse<SearchResponses<List<PlaceSearchResponse>>> searchPlaceInfoListNew(ReqPlaceSearchInfoNew reqPlaceSearchInfo) {
        PlaceSearchService placeSearchService = new PlaceSearchServiceImpl(solrUrl);
        return placeSearchService.searchPlaceInfoListNew(reqPlaceSearchInfo);
    }


        @Override
    public boolean delAllDoc(Integer type) {
        PlaceSearchService placeSearchService = new PlaceSearchServiceImpl(solrUrl);
        APIResponse response = placeSearchService.deleteAllDoc(type);
        return response.isRet();
    }

    public boolean delBatchById(List<Integer> ids) {
        PlaceSearchService placeSearchService = new PlaceSearchServiceImpl(solrUrl);
        return placeSearchService.deleteDocsByIds(ids, PlaceConstants.SOLR_INDEX_PLAN).isRet();
    }

    private List<PlaceIndexInfo> getPlaceIndexInfo(TbBizPlace tbBizPlace, PlaceIndexInfo placeIndexInfo){
        List<PlaceIndexInfo> result = new ArrayList<>();
        TbBizPlaceUnitSalePlanExample example = new TbBizPlaceUnitSalePlanExample();
        example.createCriteria().andPlaceIdEqualTo(tbBizPlace.getId()).andIsEffectiveEqualTo(Constants.IS_ENABLE_TRUE)
                .andDateGreaterThanOrEqualTo(new Date())
                .andUnitTypeEqualTo(PlaceConstants.UNIT_TYPE_BASE);//过滤过去的销售计划
        List<TbBizPlaceUnitSalePlan> tbBizPlaceUnitSalePlans = this
                .tbBizPlaceUnitSalePlanMapper.selectByExample(example);
        if (CollectionUtils.isNotEmpty(tbBizPlaceUnitSalePlans)) {
            Map<Integer, Double> map = baseService.getLPrice(tbBizPlace.getId());
            if (MapUtils.isNotEmpty(map)){
                placeIndexInfo.setPlace_lowestPrice(map.get(tbBizPlaceUnitSalePlans.get(0).getCategoryId()));
            }
            List<PlaceIndexInfo> placeIndexInfos = baseService.getPlaceSalePlanSerach(tbBizPlace,
                    PlaceConstants.SOLR_INDEX_DAYS, placeIndexInfo);
            if (CollectionUtils.isNotEmpty(placeIndexInfos)) {
                /*List<Integer> saleids = new ArrayList<>();
                for (PlaceIndexInfo pi : placeIndexInfos) {
                    saleids.add(Integer.parseInt(pi.getPlace_salePlanId()));
                    result.add(pi);
                }
               *//* if (delBatchById(saleids)) {
                }*//*
                delBatchById(saleids);*/
                for (PlaceIndexInfo pi : placeIndexInfos) {
                    result.add(pi);
                }
                deleteDocById(tbBizPlace.getId(), PlaceConstants.SOLR_INDEX_PLACE);
                deleteDocById(tbBizPlace.getId(), PlaceConstants.SOLR_INDEX_PLAN);
            }else {
                deleteDocById(tbBizPlace.getId(), PlaceConstants.SOLR_INDEX_PLACE);
                deleteDocById(tbBizPlace.getId(), PlaceConstants.SOLR_INDEX_PLAN);
                result.add(placeIndexInfo);
            }
        }else {
            deleteDocById(tbBizPlace.getId(), PlaceConstants.SOLR_INDEX_PLACE);
            deleteDocById(tbBizPlace.getId(), PlaceConstants.SOLR_INDEX_PLAN);
            result.add(placeIndexInfo);
        }
        return result;
    }

    private List<PlaceIndexInfo> getPlaceIndexInfoByids(List<String> ids, TbBizPlace tbBizPlace, PlaceIndexInfo placeIndexInfo){
        List<PlaceIndexInfo> result = new ArrayList<>();
        TbBizPlaceUnitSalePlanExample example = new TbBizPlaceUnitSalePlanExample();
        example.createCriteria().andSaleIdIn(ids).andIsEffectiveEqualTo(Constants.IS_ENABLE_TRUE).andDateGreaterThan(new Date());//过滤过去的销售计划
        List<TbBizPlaceUnitSalePlan> tbBizPlaceUnitSalePlans = this
                .tbBizPlaceUnitSalePlanMapper.selectByExample(example);
        if (CollectionUtils.isNotEmpty(tbBizPlaceUnitSalePlans)) {
            List<PlaceIndexInfo> placeIndexInfos = baseService.getPlaceSalePlanSerach(tbBizPlace,
                    PlaceConstants.SOLR_INDEX_DAYS, placeIndexInfo);
            if (CollectionUtils.isNotEmpty(placeIndexInfos)) {
                /*List<Integer> saleids = new ArrayList<>();
                for (PlaceIndexInfo pi : placeIndexInfos) {
                    saleids.add(Integer.parseInt(pi.getPlace_salePlanId()));
                    result.add(pi);
                }
                *//*if (delBatchById(saleids)) {
                }*//*
                delBatchById(saleids);*/
                for (PlaceIndexInfo pi : placeIndexInfos) {
                    result.add(pi);
                }
                deleteDocById(tbBizPlace.getId(), PlaceConstants.SOLR_INDEX_PLACE);
                deleteDocById(tbBizPlace.getId(), PlaceConstants.SOLR_INDEX_PLAN);
            }else {
                deleteDocById(tbBizPlace.getId(), PlaceConstants.SOLR_INDEX_PLACE);
                deleteDocById(tbBizPlace.getId(), PlaceConstants.SOLR_INDEX_PLAN);
                result.add(placeIndexInfo);
            }
        }
        return result;
    }

    @Override
    @Async
    public void addPlaceIndex(TbBizPlace tbBizPlace, List<String> ids) {
        if (!StringUtils.isEmpty(tbBizPlace.getId())) {
            List<PlaceIndexInfo> result = new ArrayList<>();
            try {
                if (StringUtils.isEmpty(tbBizPlace.getName())){
                    tbBizPlace = this.tbBizPlaceMapper.selectByPrimaryKey(tbBizPlace.getId());
                }
                if (StringUtils.isEmpty(tbBizPlace)){
                    return;
                }
                PlaceSearchService placeSearchService = new PlaceSearchServiceImpl(solrUrl);
                PlaceIndexInfo placeIndexInfo = baseService.tb2InsertPlaceIndexInfo(tbBizPlace);

                if (!StringUtils.isEmpty(placeIndexInfo)) {
                    if (StringUtils.isEmpty(ids)){
                        result = getPlaceIndexInfo(tbBizPlace, placeIndexInfo);
                    }
                   /* if (CollectionUtils.isNotEmpty(ids)) {
                        result = getPlaceIndexInfoByids(ids, tbBizPlace, placeIndexInfo);
                    }*/
                }
                if (CollectionUtils.isNotEmpty(result)){
                    /*long count = this.placeRedisTemplate.incrBy("place_sale_count", result.size());
                    Date curDate = new Date();
                    Date preDate = (Date)placeRedisTemplate.get("place_set_date");
                    if(preDate == null){
                        placeRedisTemplate.set("place_set_date", curDate);
                    }else {
                        long diffLongTime = curDate.getTime()-preDate.getTime();
                        long minuteFine = 5 * 1000 * 60;
                        if(diffLongTime>0 && diffLongTime>=minuteFine){
                            logger.warn("place_sale_count:【currentTime{}】,【preTime{}】,【totalCount{}】"
                                    ,curDate, preDate, count);
                        }
                    }*/
                    // 加入索引

                    placeSearchService.insertDocs(result);
                }
            } catch (Exception e) {
                logger.error("增加solr销售计划索引异常:{}", tbBizPlace.getId());
            }
        }
    }

    private boolean addPlaceIndexRedis(Integer placeid, List<PlaceIndexInfo> placeIndexInfos){
        this.placeRedisTemplate.set("place_index_sale" + placeid, placeIndexInfos);
        Date curDate = new Date();
        Date preDate = (Date)placeRedisTemplate.get("place_set_date");
        if(preDate == null){
            placeRedisTemplate.set("place_set_date", curDate);
        }else {
            long diffLongTime = curDate.getTime()-preDate.getTime();
            long minuteFine = 5 * 1000 * 60;
            if(diffLongTime>0 && diffLongTime>=minuteFine){
                //更新solr

            }
        }
        return true;
    }



    @Override
    public void addPlaceSaleIndex(Integer placeId) {
        TbBizPlace tbBizPlace = this.tbBizPlaceMapper.selectByPrimaryKey(placeId);
        this.addPlaceIndex(tbBizPlace, null);
    }

    @Override
    public APIResponse importIndex(Map map) {
        Integer pageNum = MapUtils.getInteger(map, "pageNum", 0);
        Integer pageSize = MapUtils.getInteger(map, "pageSize", 10);
        Integer isSign = MapUtils.getInteger(map, "isSign");
        Integer isShelves = MapUtils.getInteger(map, "isShelves");

        TbBizPlaceExample tbBizPlaceExample = new TbBizPlaceExample();
        TbBizPlaceExample.Criteria criteria = tbBizPlaceExample.createCriteria();
        criteria.andIsDeleteEqualTo(0);
        if (!StringUtils.isEmpty(isSign)){
            criteria.andIsSignEqualTo(isSign.byteValue());
        }
        if (!StringUtils.isEmpty(isShelves)){
            criteria.andIsShelvesEqualTo(isShelves);
        }
        int count = this.tbBizPlaceMapper.countByExample(tbBizPlaceExample);
        tbBizPlaceExample.setPage(Page.createPage(count, pageNum, pageSize));
        List<TbBizPlace> tbBizPlaces = this.tbBizPlaceMapper.selectByExample(tbBizPlaceExample);
        if (CollectionUtils.isNotEmpty(tbBizPlaces)){
            List<PlaceIndexInfo> placeIndexInfoList = new ArrayList<>();
            for (TbBizPlace tbBizPlace : tbBizPlaces){
                List<PlaceIndexInfo> tmpPlaces = getIndexByPlace(tbBizPlace);
                if (CollectionUtils.isNotEmpty(tmpPlaces)){
                    placeIndexInfoList.addAll(tmpPlaces);
                }
            }
            if (CollectionUtils.isNotEmpty(placeIndexInfoList)){
                return this.insertDocs(placeIndexInfoList);
            }
        }
        return APIResponse.returnFail("导入失败");
    }

    public boolean updatePlaceIndex(){
        Map map = new HashMap();
        TbBizPlaceExample tbBizPlaceExample = new TbBizPlaceExample();
        TbBizPlaceExample.Criteria criteria = tbBizPlaceExample.createCriteria();
        criteria.andIsDeleteEqualTo(0).andIsSignEqualTo(PlaceConstants.IS_SIGN_YES);
        int count = this.tbBizPlaceMapper.countByExample(tbBizPlaceExample);
        if (count > 0){
            int num = 0;
            if (count % 50 == 0){
                num =  count / 50;
            }else {
                num = count / 50 + 1;
            }
            for (int i=0; i<num; i++){
                map.put("pageNum", i);
                map.put("pageSize", 50);
                map.put("isSign", 1);
                importIndex(map);
            }
            return true;
        }
        return false;
    }

    private List<PlaceIndexInfo> getIndexByPlace(TbBizPlace tbBizPlace){
        PlaceIndexInfo placeIndexInfo = baseService.tb2InsertPlaceIndexInfo(tbBizPlace);

        if (!StringUtils.isEmpty(placeIndexInfo)) {
            return getPlaceIndexInfo(tbBizPlace, placeIndexInfo);
        }
        return null;
    }

    public boolean importPlaceIndexDays(Integer days) {
        try {
            PlaceSearchService placeSearchService=new PlaceSearchServiceImpl(solrUrl);

            List<PlaceIndexInfo> inserPlaceIndexInfos = new ArrayList<>();
//        PlaceIndexInfo placeIndexInfo = this.searchService.getPlaceIndexInfo(12700);
            TbBizPlaceExample example = new TbBizPlaceExample();
            example.createCriteria().andIsDeleteEqualTo(0).andSourceIdEqualTo(2);
            List<TbBizPlace> tbBizPlaces = this.tbBizPlaceMapper.selectByExample(example);
            for (TbBizPlace tbBizPlace : tbBizPlaces) {
                PlaceIndexInfo placeIndexInfo = baseService.tb2InsertPlaceIndexInfo(tbBizPlace);

                List<PlaceIndexInfo> placeIndexInfos = baseService.getPlaceSalePlanSerach(tbBizPlace,
                            days, placeIndexInfo);
                    if (CollectionUtils.isNotEmpty(placeIndexInfos)) {
                        List<Integer> saleids = new ArrayList<>();
                        for (PlaceIndexInfo pi : placeIndexInfos){
                            saleids.add(Integer.parseInt(pi.getPlace_salePlanId()));
                            inserPlaceIndexInfos.add(pi);
                        }
                        if (delBatchById(saleids)){
                        }
                    }
            }
            placeSearchService.insertDocs(inserPlaceIndexInfos);
            return true;
        }catch (Exception e){
            return false;
        }
    }

    public boolean delIndexBefore14Day() {
        PlaceSearchService placeSearchService=new PlaceSearchServiceImpl(solrUrl);
        APIResponse<List<PlaceIndexInfo>> resp = placeSearchService.searchPlaceInfoBefore14Day();
        if (resp.isRet()){
            List<PlaceIndexInfo> list = resp.getData();
            if (CollectionUtils.isNotEmpty(list)){
                List<Integer> ids = new ArrayList<>();
                for (PlaceIndexInfo placeIndexInfo : list){
                    ids.add(Integer.parseInt(placeIndexInfo.getPlace_salePlanId()));
                }
                APIResponse result = placeSearchService.deleteDocsByIds(ids, PlaceConstants.SOLR_INDEX_PLAN);
                return result.isRet();
            }
        }
        logger.error("删除过期索引失败:{}", resp.getErrmsg());
        return false;
    }


    public APIResponse deleteDocById(Integer placeId, Integer type){
        PlaceSearchService placeSearchService = new PlaceSearchServiceImpl(solrUrl);
        return placeSearchService.deleteDocById(placeId, type);
    }

    public APIResponse insertDocs(final List<PlaceIndexInfo> insertPlaceIndexInfos){
        PlaceSearchService placeSearchService=new PlaceSearchServiceImpl(solrUrl);
        return placeSearchService.insertDocs(insertPlaceIndexInfos);
    }
}
